<template>
  <div class="login">
    <el-card class="box-card">
      <template #header>
        <div class="clearfix">
          <span>学生后台管理系统</span>
        </div>
      </template>
      <el-form
        label-width="80px"
        :model="form"
        ref="form"
        :rules="rules"
      >
        <el-form-item
          label="用户名"
          prop="username"
        >
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input
            type="password"
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="logins()"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import { nameRule, passRule } from '../utils/vaildate.js'
import { setToken } from '@/utils/setToken.js'
import { login } from '@/api/api.js'
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ validator: nameRule, trigger: 'blur' }],
        password: [{ validator: passRule, trigger: 'blur' }]
      }
    }
  },
  methods: {
    login(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          console.log(this.form)
          /*  把这个登录房费封装成api进行调用
           this.service.post('/login', this.form)
            .then((res) => {
                if (res.data.status === 200) {
                    setToken('username', res.data.username)
                    setToken('token', res.data.token)
                    this.$message({message: res.data.message, type: 'success'})
                    this.$router.push('/home')
                }
            })
            */
          login(this.form).then((res) => {
            if (res.data.status === 200) {
              setToken('username', res.data.username)
              setToken('token', res.data.token)
              this.$message({ message: res.data.message, type: 'success' })
              this.$router.push('/home')
            }
          })
        } else {
          console.error(this.form)
        }
      })
    },
    logins() {
      console.log(this.form)
      if (this.form.username === 'sa' && this.form.password === '1') {
        this.$router.push('/home')
      } else if (this.form.username === '' || this.form.password == '') {
        alert(`账号密码是sa/1`)
      } else {
        alert(
          `账号密码是sa/1您输入的是${this.form.username}和${this.form.password}`
        )
      }
    }
  }
}
</script>
<style lang="less">
.login {
  width: 100%;
  height: 100%;
  position: absolute;
  background: url('../assets/bg2.jpg') center no-repeat;
  .el-card {
    background: #65768557;
  }
  background-size: cover;
  .box-card {
    width: 450px;
    margin: 200px auto;
    color: #fff;
    .el-form .el-form-item__label {
      color: #fff;
    }
    .el-card__header {
      font-size: 34px;
    }
    .el-button {
      width: 100%;
    }
  }
}
</style>
